package com.xxx.auth.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xxx.auth.entity.enums.Status;
import com.xxx.auth.entity.enums.UserType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Entity
@Comment("用户表")
@Table(name = "t_user")
@EntityListeners(AuditingEntityListener.class)
public class User extends TimeEntity implements Serializable {

    public static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "CustomUUID")
    @GenericGenerator(name = "CustomUUID", strategy = "com.xxx.auth.common.generator.CustomUUIDGenerator")
    @Column(name = "id", unique = true)
    private String id;

    @Column(nullable = false, unique = true, length = 95)
    @Comment("用户名")
    @Schema(title = "用户名")
    private String username;

    @Column(nullable = false, length = 95)
    @Comment("密码")
    @Schema(title = "密码")
    private String password;

    @Column(nullable = false, length = 95)
    @Comment("姓名")
    @Schema(title = "姓名")
    private String name;

    @Column
    @Comment("系统管理员：ADMINISTRATOR 普通用户：USER")
    @Schema(title = "系统管理员：ADMINISTRATOR 普通用户：USER")
    @Enumerated(EnumType.STRING)
    private UserType userType = UserType.USER;

    @Column
    @Comment("NORMAL：正常 DELETED：删除 INACTIVE：停用")
    @Schema(title = "NORMAL：正常 DELETED：删除 INACTIVE：停用")
    @Enumerated(EnumType.STRING)
    private Status status = Status.NORMAL;

    @ManyToMany(mappedBy = "userSet", fetch = FetchType.LAZY)
    @JsonIgnore
    private Set<Application> applicationSet = new HashSet<>();

}
